package com.zch.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.user.entity.User;
import com.zch.user.service.UserService;
import com.zch.user.util.PageUtil;
import com.zch.user.util.PasswordLock;
import com.zch.user.util.XmlSession;

public class UserServiceImpl extends XmlSession implements UserService {
	// 加载配置文件
	public void init() {
		getSession("mybatis-config.xml");
	}

	@Override
	public List<User> getAllUserByPage(String uName, String rName, String tel, int pageIndex) {
		init();
		// 使用map存放参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uName", uName);
		map.put("rName", rName);
		map.put("tel", tel);
		int start = (pageIndex - 1)*PageUtil.PAGE_SIZE; 
		int pageSize = PageUtil.PAGE_SIZE;
		map.put("start", start);
		map.put("pageSize", pageSize);
		List<User> list = userMapper.getAllUserByPage(map);
		// 关闭session对象
		closeSession();
		return list;
	}

	@Override
	public int getTotalPages(String uName, String rName, String tel) {
		init();
		// 使用map存放参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uName", uName);
		map.put("rName", rName);
		map.put("tel", tel);
		// 获得总数据量
		int counts = userMapper.getTotalCounts(map);
		// 获得总页码
		int totalPages = counts % PageUtil.PAGE_SIZE == 0 ? 
				counts / PageUtil.PAGE_SIZE: counts / PageUtil.PAGE_SIZE + 1;
		// 关闭session对象
		closeSession();
		return totalPages;
	}

	@Override
	public int getTotalCounts(String uName, String rName, String tel) {
		init();
		// 使用map存放参数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uName", uName);
		map.put("rName", rName);
		map.put("tel", tel);

		// 获得总数据量
		int counts = userMapper.getTotalCounts(map);
		// 关闭session对象
		closeSession();
		return counts;
	}

	@Override
	public boolean addUser(User user) {
		init();
		userMapper.addUser(user);
		closeSession();
		return true;
	}

	@Override
	public boolean deleteUser(int uId) {
		init();
		return userMapper.deleteUser(uId);
	}

	@Override
	public boolean updateUser(User user) {
		init();
		return userMapper.updateUser(user);
	}

	@Override
	public Map<String, Object> doLogin(String uName, String password) {
		init();
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = userMapper.getAllUser();
		// 关闭session对象
		closeSession();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (uName.equals(list.get(i).getuName())) {// 用户名存在
					// 判断密码是否正确
					for (int j = i; j < list.size(); j++) {
						if (PasswordLock.Md5Lock1(password).equals(list.get(i).getPassword())) {// 密码正确
							map.put("isLogin", true);// 登录成功
							map.put("user", list.get(i));// 存入用户对象
							return map;
						}
						if (j == list.size() - 1) {

							map.put("isLogin", false);// 登录失败
							map.put("msg", "密码错误!");
							return map;
						}
					}
				}
				if (i == list.size() - 1) {// 用户名不存在
					map.put("isLogin", false);// 登录失败
					map.put("msg", "用户名不存在!");
					return map;
				}
			}
		}
		return map;
	}

	@Override
	public User getUser(int uId) {
		init();
		User user = userMapper.getUser(uId);
		// 关闭session对象
		closeSession();
		return user;
	}

	@Override
	public List<User> getAllUser() {
		init();
		List<User> list = userMapper.getAllUser();
		// 关闭session对象
		closeSession();
		return list;
	}

}
